package com.dandan.mobilelover.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.dandan.mobilelover.component.Pagination;
import com.dandan.mobilelover.entity.Book;
import com.dandan.mobilelover.entity.Category;
import com.dandan.mobilelover.entity.Image;
import com.dandan.mobilelover.service.CategoryService;
import com.dandan.mobilelover.service.ImageService;

@RequestMapping("/data_mgr")
@SessionAttributes("currentUser")
@Controller
public class ImageManageController {
	
	@Inject
	private Pagination page;
	
	@Inject
	private CategoryService categoryService;
	
	@Inject
	private ImageService imageService;
	
	@RequestMapping(value="/image/new", method=RequestMethod.GET)
	public String new_book(Model model){
		List<Category> categories = categoryService.findByType(Category.TYPE[1]);
		model.addAttribute("categories", categories);
		return "data_import/image/new";
	}
	
	@RequestMapping(value="/image", method= RequestMethod.GET)
	public String book_import(HttpServletRequest request, Model model){
		if(request.getParameter("p") == null){
			page.setCurrentPage(1);
		}else{
			page.setCurrentPage(Integer.parseInt(request.getParameter("p")));
		}
		List<Image> images = imageService.searchList(page);
		page.setBaseUrl(request.getRequestURI());
		model.addAttribute("image_list", images);
		model.addAttribute("page", page);
		return "data_import/image/image_import";
	}
}
